<?php

namespace App\HttpController\Admin;

use App\Model\ImagesModel;
use App\Utility\Filesystem;

class ResourceController extends AuthController {


    public function index(){
        $request= $this->request();
        $page   = $request->getRequestParam('page') ?: 1;
        $kwd    = $request->getRequestParam('kwd') ?: '';
        $model  = new ImagesModel();
        $list   = $model->select('images.*,a.nickname')
            ->where(function ($query) use($kwd){
                if($kwd) $query->whereLike('hash',$kwd);
            })->join('admins as a','a.id = images.admin_id')
            ->orderBy('created_at')
            ->paginate($page,8);
        return $this->writeJson(0,$list);
    }

    public function destroy(){
        $request = $this->request();
        $id = $request->getRequestParam('id');
        $model = new ImagesModel();
        //先删除七牛云的图片资源
        $list = $model->select('path')->whereIn('id',$id)->get();
        $paths = [];
        foreach ($list as $item){
            $paths[] = $item['path'];
        }
        $res = (new Filesystem())->qiNiuBatch($paths);
        if($res){
            $model->whereIn('id',$id)->delete();
            return $this->writeJson(0);
        }
        return $this->writeJson(1,$res,'删除失败');
    }

}
